from invidious import *
import requests
import sys

from plugins.core.plugin import Plugin
from plugins.core.pluginvideo import PluginVideo
from plugins.core.pluginplaylist import PluginPlaylist

sys.path.append("..")
from src.vobjects.item import Item
from src.vobjects.video import Video
from src.vobjects.channel import Channel
from src.vobjects.playlist import Playlist

from src.utils import *
from src.history import History

class InvidiousTrending(Plugin):

    def __init__(self, history: History=History()):
        super().__init__(history=history)

        self.name = "Invidious: Trending"
        self.is_query_required = True
        self.query_description = """Please, input args (separate they with &) or leave this field empty.
    type: Music, Gaming, News, Movies (Ex: type=News)  
    region: your region (Ex: region=US)  

    Example: type=music&region=US    
"""

        self.flag = "-it"
        self.full_flag = "--ivtrending"
        self.flag_help = "show trending videos from invidious"
        self.flag_action = ""

    def get_trending(self, query: str=""):  
        dct = argstr_to_dict(query) 
        trending = self.invidious.get_trending(**dct)

        items = []
        for video in trending:
            if video == None: continue
            video.convert(Video)
            video.url = self.invidious.get_working_mirror() + "/watch?v=" + video.videoId
            inHistory = video.in_history(self.history)
            item = video.to_item(inHistory=inHistory)
            items.append(item)

        return items

    def get_items_by_query(self, query: str=""):
        return self.get_trending(query=query)

    def get_items(self):
        return list()

if __name__ == "__main__":
    al = InvidiousTrending()

    items = al.get_items_by_query(query="мастера меча онлайн")
    for item in items:
        print(item.title, item.parent.url)




